<?php
/**
* @package HelloWorld
* @version 1.0
* @copyright Copyright (C) 2005 Open Source Matters. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* Joomla! is free software and parts of it may contain or be derived from the
* GNU General Public License or other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

$AppVersion = "ExtensionIcons_OneFive 0.2m";
global $mainframe;
$cid 		= JRequest::getVar( 'cid', array(0), '', 'array' );
if (!is_array( $cid ))
{
	$cid = array(0);
}

require_once( JApplicationHelper::getPath( 'admin_html' ) ); 
require_once( JApplicationHelper::getPath( 'class' ) ); 

/*
 * Make sure the user is authorized to view this page
 */

/* 
$user = & JFactory::getUser();
   
if (!$user->authorize( 'com_helloword', 'manage' )) {
	$mainframe->redirect( 'index.php', JText::_('ALERTNOTAUTH') );
}
*/
// Load the html class
// DEVNOTE: This will include the admin.helloworld.html.php file, 
// so now we can use anything that it provides!

/*
 * DEVNOTE: $task and $option are obviously defined outside of the scope of our code.
 * The different functions mentioned below are all defined further on.
 * 
 * We defined class helloScreens and 4 function
 * helloworld,helloagain,hellotestfoo,hellodefault
 * 
 * Each of them does nothing else than shows different text.  
 * 
 * $task values are defined in com_helloworld.xml
 *  
 * 		<submenu>
 *			<!-- Note that all & must be escaped to &amp; for the file to be valid XML and be parsed by the installer -->
 *			<menu link="option=com_helloworld&amp;task=helloworld">Hello World!</menu>
 *			<menu link="option=com_helloworld&amp;task=helloagain">Hello Again!</menu>
 *			<menu link="option=com_helloworld&amp;task=hellotestfoo">Testing Foo Function</menu>
 *		</submenu>    
 */ 
 echo JRequest::getVar('task');
switch (JRequest::getVar('task'))
{
  case 'helloworld':
    HTML_ExtensionIcons::helloworld();
    break;
  case 'helloagain': 
    HTML_ExtensionIcons::helloagain();
    break;
  case 'hellotestfoo': 
    HTML_ExtensionIcons::hellotestfoo();
    break;
    
       	case "exiconRefresh":
   	  	synchronizeme ( $option , $AppVersion);
   	  	break;
   	case "ConfigModule":
   		ConfigModule ( $option );
   		break;
    case "listItems":
      	listItems( $option , $AppVersion );
      	break;
    case "edit":
      	edit( $option , $cid[0], $AppVersion  );
      	break;
    case "add":
    	//die("KK");
    	edit( $option , '', $AppVersion );
    	break;
    case "save":
     	save( $option );
     	break;
    case "remove":
    	delete( $option , $cid );
    	break;
    
    case "publish":
     	publish( $option , '1' , $cid );
     	break;
    case "unpublish":
     	publish( $option , '0' , $cid );
     	break;
    case 'orderup':
		orderExIcons( intval( $cid[0] ), -1, $option );
		break;
	case 'orderdown':
		orderExIcons( intval( $cid[0] ), 1, $option );
		break;
	case "MediaManager":
		//HTML_ExtensionIcons::WrapMediaManager();
		//index.php?option=com_media&task=list&tmpl=component&folder=stories/food
		$mainframe->redirect( "index.php?option=com_media&folder=stories/food");//ExtensionIcons_custom" );

	
	default:
	echo '<h1>'.JRequest::getVar('task').'</h1>';
	listItems ( $option, $AppVersion);
//	helloScreens::hellodefault();
	break;
}

function listItems( $option , $AppVersion )
{
	global $database, $mainframe, $mosConfig_list_limit;


//1.0   $limit 		= intval( $mainframe->getUserStateFromRequest( "viewlistlimit", 'limit', $mosConfig_list_limit ) );
//1.0	$limitstart = intval( $mainframe->getUserStateFromRequest( "view{$option}limitstart", 'limitstart', 0 ) );
	$limit		= $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 0);
	$limitstart = $mainframe->getUserStateFromRequest( $option.'limitstart', 'limitstart', 0 );

	$database = & JFactory::getDBO();//
    
    $database->setQuery("SELECT COUNT(*) FROM #__extension_icons");
    $total = $database->loadResult();

	if ( $total == 0 )
	{
		echo _EXICONS_NO_EXICONS;
		return;
	}

	jimport('joomla.html.pagination');
	$pageNav = new JPagination( $total, $limitstart, $limit );

 	//require_once( $GLOBALS['mosConfig_absolute_path'] . '/administrator/includes/pageNavigation.php' );
	//$pageNav = new mosPageNav( $total, $limitstart, $limit  );

     $database->setQuery("SELECT * FROM #__extension_icons"
                         ." ORDER BY ordering", $pageNav->limitstart, $pageNav->limit );
     $rows = $database->loadObjectList();

     HTML_ExtensionIcons::listItems($option, $rows, $pageNav);
     HTML_ExtensionIcons::printFooter( $AppVersion );
}//function
function orderExIcons( $uid, $inc, $option )
{
	global $mainframe;

	$database = & JFactory::getDBO();//
	//$row	=& JTable::getInstance('category' );

	$row = new mosExtensionIcons( $database );
	$row->load( $uid );
	$row->reorder();
	$row->move( $inc, "published >= 0" );
	$row->reorder();

	$mainframe->redirect( 'index2.php?option=' . $option );
}

function publish( $option, $publish ,$cid )
{
	global $mainframe;

	$database = & JFactory::getDBO();//

     $cids = implode( ',', $cid );
     $database->setQuery( "UPDATE #__extension_icons "
                         ."SET published='$publish' "
                         ."WHERE id IN ($cids)" );
     $database->query();
     $mainframe->redirect( 'index2.php?option=' . $option );
}

function edit( $option, $uid , $AppVersion )
{
//die("JJ");
	$database = & JFactory::getDBO();//
     $row = new mosExtensionIcons( $database );
     $row->load($uid);
     HTML_ExtensionIcons::edit( $option, $row );
     HTML_ExtensionIcons::printFooter( $AppVersion );
}

function save( $option )
{
	global $mainframe;
	$database = & JFactory::getDBO();//
     $row = new mosExtensionIcons ( $database );
     $row->bind( $_POST );
     $row->store();
     $mainframe->redirect( "index2.php?option=$option", _EXICONS_SAVED );
}

function delete( $option, $cid )
{
	global $mainframe;
	$database = & JFactory::getDBO();//
     $cids = implode( ',', $cid );
     $database->setQuery( "DELETE FROM #__extension_icons "
                         ."WHERE id IN ($cids)" );
     $database->query();
	$mainframe->redirect( "index2.php?option=$option" );
}

function ConfigModule ( $option )
{
	global $mainframe;

	$database = & JFactory::getDBO();//
    $database->setQuery("SELECT id FROM #__modules"
                    ."\n WHERE module='mod_extensioniconsonefive'");
 	$id = $database->loadResult();
 	//?option=com_modules&client=1&task=edit&cid=43
	$mainframe->redirect("index2.php?option=com_modules&client=1&task=edit&hidemainmenu=1&cid[]=".$id);
}

function synchronizeme ( $option , $AppVersion)
{
	global $mainframe;

	$database = & JFactory::getDBO();//
     echo JText::_('EXICONS_SYNC_START') . ' ';
     $database->setQuery("SELECT * FROM #__extension_icons"
                         ." ORDER BY id");
     $backendicons = $database->loadObjectList();

     $database->setQuery("SELECT * FROM #__components"
     					." WHERE admin_menu_link != ''"
                         ." ORDER BY id");
    $db_components = $database->loadObjectList();
	$cnt_new = 0;
     foreach ($db_components as $db_component)
     {
     	$found = false;
     	if ( count($db_components > 0))
     	{
	     	foreach ($backendicons as $backendicon)
   		  	{
				if ( $backendicon->admin_menu_link == $db_component->admin_menu_link )
				{
					$found = true;
				}
     		}
    	 }
    	 if ($found == false)
    	 {
    	 	$parent_name = '';
    	 	if ($db_component->parent > 0)
    	 	{
    		 	$query = "SELECT name FROM #__components WHERE id = " . $db_component->parent;
   		 	 	$database->setQuery($query);
   		 	 	$parent_name = $database->loadResult();
    		 }
			$query = "INSERT INTO #__extension_icons"
			."\n SET name = '$db_component->name',"
			."\n parent = $db_component->parent,"
			."\n admin_menu_link = '$db_component->admin_menu_link',"
			."\n admin_menu_alt = '$db_component->admin_menu_alt',"
			."\n `linkoption` = '$db_component->option',"
			."\n admin_menu_img = '$db_component->admin_menu_img'";
			if ($parent_name != '')
			{
				$query .= ",\n parent_name = '$parent_name'";
			}
   		 	$database->setQuery($query);
   		    $database->query();
			$cnt_new += 1;
    	 }//if
     }//foreach
	echo $cnt_new . ' ' . JText::_('EXICONS_SYNC_END') . ' ' . '<img src="images/tick.png" />';
	listItems ( $option , $AppVersion );
}//function

?>
